package com.action.pc.order;

import com.bean.*;
import com.common.BaseAction;
import com.common.Cache;
import com.common.Express;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.service.dis.DisCompanyService;
import com.service.member.MbAccountService;
import com.service.order.OrderPayService;
import com.service.order.OrderService;
import com.service.product.ProductService;
import com.util.KdniaoApi;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lvjianqing on 2017/9/10.
 */
@Controller
@RequestMapping("order")
public class OrderInfoAction extends BaseAction {
    Logger log = Logger.getLogger(OrderInfoAction.class);
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderPayService orderPayService;
    @Autowired
    private DisCompanyService disCompanyService;
    @Autowired
    private MbAccountService mbAccountService;
    @Autowired
    private ProductService productService;

    @RequestMapping("list")
    public ModelAndView list(ModelMap modelMap, OrderInfoVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        filterParaMap(paraMap);
        paraMap.put("orderno", vo.getOrderno());
        paraMap.put("orderstatus", vo.getOrderstatus());
        paraMap.put("ordertype", vo.getOrdertype());
        paraMap.put("distype", vo.getDistype());
        paraMap.put("starttime", vo.getStarttime());
        paraMap.put("endtime", vo.getEndtime());
        Page page = orderService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/order_list", modelMap);
    }

    /**
     * 发货页面
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("send")
    public ModelAndView send(HttpServletRequest request, ModelMap modelMap, OrderInfoVo vo) {
        Integer id = vo.getId();
        Order_info order = new Order_info();
        if (id != null) {
            order = orderService.findOrderById(id);
            if (order != null) {
                String orderno = order.getOrderno();
                Order_pay pay = orderPayService.selectByOrderNo(orderno);
                modelMap.put("pay", pay);
                Integer dcid = order.getDiscompany();
                if(dcid!=null){
                    Dis_company dc = disCompanyService.selectByPrimaryKey(dcid);
                    modelMap.put("dc", dc);
                }
                List<OrderProductVo> opList = orderService.getOrderProductByOrderNo(orderno);
                modelMap.put("opList", opList);
            }
        }
        List<Dis_company> dislist = disCompanyService.findAll();
        modelMap.put("dislist", dislist);
        modelMap.put("order", order);
        return new ModelAndView("/pc/order_send", modelMap);
    }

    /**
     * 发货
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("dosend")
    @ResponseBody
    public String dosend(HttpServletRequest request, ModelAndJson modelMap, OrderInfoVo vo) {
        Integer id = vo.getId();
        if (id != null) {
            Order_info order = orderService.findOrderById(id);
            if (order != null) {
                Integer discompany = vo.getDiscompany();
                String disno = vo.getDisno();
                BigDecimal disamount = vo.getDisamount();
                if (discompany != null && disamount != null && disno != null) {
                    order.setDiscompany(discompany);
                    order.setDisamount(disamount);
                    order.setDisno(disno);
                    orderService.saveSend(order);
                    if (order.getOrderstatus() == 1) {
                        modelMap.setTrue("发货成功");
                    } else if (order.getOrderstatus() == 2) {
                        modelMap.setTrue("修改成功");
                    }
                } else {
                    modelMap.setFalse("-1", "物流信息不完整");
                }
            } else {
                modelMap.setFalse("-1", "订单不存在");
            }
        } else {
            modelMap.setFalse("-1", "操作失败");
        }
        return modelMap.toString();
    }

    /**
     * 退款、退货页面
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("back")
    public ModelAndView back(HttpServletRequest request, ModelMap modelMap, OrderInfoVo vo) {
        Integer id = vo.getId();
        Order_info order = new Order_info();
        if (id != null) {
            order = orderService.findOrderById(id);
            if (order != null) {
                String orderno = order.getOrderno();
                Order_pay pay = orderPayService.selectByOrderNo(orderno);
                modelMap.put("pay", pay);

                List<OrderProductVo> opList = orderService.getOrderProductByOrderNo(orderno);
                modelMap.put("opList", opList);
            }
        }
        if (order.getDiscompany() != null) {
            Dis_company dis = disCompanyService.selectByPrimaryKey(order.getDiscompany());
            modelMap.put("dis", dis);
        }
        modelMap.put("order", order);
        return new ModelAndView("/pc/order_back", modelMap);
    }

    /**
     * 退款/退货
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("doback")
    @ResponseBody
    public String doback(HttpServletRequest request, ModelAndJson modelMap, OrderInfoVo vo) {
        Integer id = vo.getId();
        if (id != null) {
            Order_info order = orderService.findOrderById(id);
            if (order != null) {
                Integer memberid = order.getMemberid();
                orderService.saveBack(order);
                if (order.getOrderstatus() == 7) {
                    modelMap.setTrue("退款成功");
                } else if (order.getOrderstatus() == 8) {
                    modelMap.setTrue("退货成功");
                }
            } else {
                modelMap.setFalse("-1", "订单不存在");
            }
        } else {
            modelMap.setFalse("-1", "操作失败");
        }
        return modelMap.toString();
    }

    /**
     * 订单详情页
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(HttpServletRequest request, ModelMap modelMap, OrderInfoVo vo) {
        Integer id = vo.getId();
        Order_info order = new Order_info();
        if (id != null) {
            order = orderService.findOrderById(id);
            if (order != null) {
                String orderno = order.getOrderno();
                Order_pay pay = orderPayService.selectByOrderNo(orderno);
                modelMap.put("pay", pay);

                List<OrderProductVo> opList = orderService.getOrderProductByOrderNo(orderno);
                modelMap.put("opList", opList);
            }
        }
        if (order.getDiscompany() != null) {
            Dis_company dis = disCompanyService.selectByPrimaryKey(order.getDiscompany());
            modelMap.put("dis", dis);
        }
        modelMap.put("order", order);
        return new ModelAndView("/pc/order_info", modelMap);
    }

    /**
     * 物流信息
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("express")
    public ModelAndView express(HttpServletRequest request, ModelMap modelMap, String disNo, String companyName) {
        if (StringUtil.isNotEmpty(disNo) && StringUtil.isNotEmpty(companyName)) {
            String expCode = Cache.disCompanyMap.get(companyName);
            try {
                Express e = KdniaoApi.instance().getOrderTraces(expCode, disNo);
                modelMap.put("express", e);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ModelAndView("/pc/express", modelMap);
    }

}
